<html>
<head><meta charset="utf-8"><title>Analysis within macro_rules · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html">Analysis within macro_rules</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="206813319"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/206813319" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#206813319">(Aug 13 2020 at 13:18)</a>:</h4>
<p><span class="user-mention" data-user-id="133169">@matklad</span> I was thinking of trying to tackle at least partial analysis within macro_rules! bodies. My thought was that we could guarantee resolution of items like <code>$crate</code> and the paths that follow. Is there any existing issue based on that, or do you have any thoughts on whether we should approach something like that right now?</p>



<a name="206824383"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/206824383" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#206824383">(Aug 13 2020 at 14:51)</a>:</h4>
<p>I think the best approach would be to do it like how I proposed in <a href="#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/macro_rules.20highlighting">https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/macro_rules.20highlighting</a></p>



<a name="206824396"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/206824396" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#206824396">(Aug 13 2020 at 14:51)</a>:</h4>
<p>But there's no issue created for this yet.</p>



<a name="206824629"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/206824629" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#206824629">(Aug 13 2020 at 14:53)</a>:</h4>
<p>I know that topic talks about analyzing content inside <code>macro!</code> but it is relevant to also analyzing content inside <code>macro_rules!</code></p>



<a name="206934479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/206934479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#206934479">(Aug 14 2020 at 14:25)</a>:</h4>
<p>I looked into it this morning. The biggest issue is that the body of a macro_rules macro is just a token tree, and there's currently no analysis done if the macro itself isn't called. <span class="user-mention" data-user-id="133169">@matklad</span> could we sync at some point to go over a best path forward here? I have a couple ideas, but not sure which is the best approach:</p>
<ul>
<li>Add rules for <code>macro_rules</code> to the ungrammar<ul>
<li>I imagine this would at least double the size of the ungrammar since it has to account for metavars within nearly all positions of the AST</li>
</ul>
</li>
<li>Add a seperate ungrammar for bodies of <code>macro_rules</code> that "forwards" the kinds of the metavars into the AST</li>
<li>Manually walk the token tree since there's a limited number of cases where goto/hover/syntax highlighting can be useful:<ul>
<li>We could only recognize <code>NameRef</code>'s external to the macro if they were part of a <code>$crate</code> path</li>
<li>We could recognize that metavars are namerefs for the macro pattern</li>
<li>Might not be able to support going to a def within the macro from a name_ref</li>
</ul>
</li>
</ul>



<a name="207250967"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/207250967" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#207250967">(Aug 18 2020 at 12:07)</a>:</h4>
<p>I don't think it makes sense to do any kind of "fancy" analysis of macro bodies any time soon</p>



<a name="207250988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/207250988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#207250988">(Aug 18 2020 at 12:07)</a>:</h4>
<p>This is a deeply heurisitc thing, and it's not like we got the basic stuff working</p>



<a name="207251050"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/207251050" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#207251050">(Aug 18 2020 at 12:08)</a>:</h4>
<p>What can, and should be done though, is supporting goto definition and compleiton for meta vars</p>



<a name="207251066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/207251066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#207251066">(Aug 18 2020 at 12:08)</a>:</h4>
<p>clicking on <code>$foo</code> should bring one to <code>$foo:expr</code></p>



<a name="207251106"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/207251106" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#207251106">(Aug 18 2020 at 12:09)</a>:</h4>
<p>I am not sure how to best achieve this, as macro machinery fundametally works with token trees, and not the syntax trees</p>



<a name="207251144"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/207251144" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#207251144">(Aug 18 2020 at 12:09)</a>:</h4>
<p>I think the right approach here is just a huge wad of code in <code>ide</code> which uses <code>TokenID</code> to switch bridge the two representations</p>



<a name="207259226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/207259226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#207259226">(Aug 18 2020 at 13:32)</a>:</h4>
<p>Could we at least have the ungrammar account for this within macro rules? <a href="https://doc.rust-lang.org/reference/macros-by-example.html">https://doc.rust-lang.org/reference/macros-by-example.html</a></p>



<a name="207259273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/207259273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#207259273">(Aug 18 2020 at 13:32)</a>:</h4>
<p>I noticed it's not in the official grammar (the one being produced by wg-grammar), but only in the reference linked above.</p>



<a name="207259374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/207259374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#207259374">(Aug 18 2020 at 13:33)</a>:</h4>
<p>I don't think this should be handle by ungrammar -- macros are token trees</p>



<a name="207259640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/207259640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#207259640">(Aug 18 2020 at 13:35)</a>:</h4>
<p>Ok, so <code>ide</code> would be the area to handle <code>macro_rules</code> in a special way?</p>



<a name="207259720"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/207259720" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#207259720">(Aug 18 2020 at 13:36)</a>:</h4>
<p>yup</p>



<a name="207259742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/207259742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#207259742">(Aug 18 2020 at 13:36)</a>:</h4>
<p>IDE would call into mbe to make sense of macro rules invocations</p>



<a name="207259784"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/207259784" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#207259784">(Aug 18 2020 at 13:36)</a>:</h4>
<p>should also stress our IR-mapping infrastructure (not that we have any :D ), as mbe are essentially are a separate IR</p>



<a name="207259991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/207259991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Paul Faria <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#207259991">(Aug 18 2020 at 13:38)</a>:</h4>
<p>I did not even realize <code>mbe</code> existed. Ok I'll review this for the time being. Thanks for following up!</p>



<a name="209715826"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/209715826" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#209715826">(Sep 10 2020 at 22:07)</a>:</h4>
<p>Can someone please tag this issue as macro? <a href="https://github.com/rust-analyzer/rust-analyzer/issues/5972">https://github.com/rust-analyzer/rust-analyzer/issues/5972</a></p>



<a name="209742442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Analysis%20within%20macro_rules/near/209742442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Analysis.20within.20macro_rules.html#209742442">(Sep 11 2020 at 05:37)</a>:</h4>
<p>Done</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>